<?php
    //require section
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_document.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_invoice.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
    $title = 'Xác Nhận Tạo Đơn Hàng';
?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
    ?>
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'menu.php';
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'slider.php';

        if (!empty($_POST['destroy'])) {
            $_SESSION['cart'][$_SESSION['user']] = array();
            lib_redirect::Redirect('/index.php');
        }

        if (!empty($_POST['create_invoice'])) {
            $list = $_SESSION['cart'][$_SESSION['user']];
            api_invoice::create_invoice($list, $_SESSION['user']);
            $_SESSION['cart'][$_SESSION['user']] = array();
            lib_redirect::Redirect('/index.php');
        }
    ?>

    <section id="main-content" class="ts-body-container uk-panel uk-panel-box ts-padding-large">
        <form action="?" method="POST" id="form_destroy_cart">
            <input type="hidden" name="destroy" value="1"/>
        </form>
        <form action="?" method="POST" id="form_create_invoice">
            <input type="hidden" name="create_invoice" value="1"/>
        </form>
        <div class="uk-text-center uk-text-bold">Danh Sách Bài Tập Muốn Mua</div>
        <table id="cart_table" class="uk-table uk-table-hover">
            <thead>
            <tr>
                <th>STT</th>
                <th>Mã bài tập</th>
                <th>Tên bài tập</th>
                <th>Giá</th>
            </tr>
            </thead>
            <tbody>
            <?php
                $list = array();
                if (!empty($_SESSION['cart'][$_SESSION['user']])) {
                    $list = $_SESSION['cart'][$_SESSION['user']];
                }
                $total_money = 0;
                foreach ($list as $num => $item_id) {
                    $item = api_document::get_document_by_id($item_id);
                    $total_money += $item['doc_price'];
                    ?>
                    <tr>
                        <td><?php echo $num + 1; ?></td>
                        <td><?php echo $item['doc_id']; ?></td>
                        <td><?php echo $item['doc_name']; ?></td>
                        <td class="uk-text-bold"><?php echo number_format($item['doc_price'], 0, '.', ','); ?> vnđ</td>
                    </tr>
                <?php
                }?>
            </tbody>
            <tfoot>
            <tr>
                <td colspan="3" class="uk-text-large">Tổng cộng:</td>
                <td class="uk-text-bold uk-text-large"><?php echo number_format($total_money, 0, '.', ','); ?> vnđ</td>
            </tr>
            </tfoot>
        </table>
        <div class="uk-text-center">
            <a href="cart.php#cart_table" class="uk-button">Quay lại giỏ hàng</a>
            <input type="submit" onclick="confirmDestroyCart();" name="btn_destroy" class="uk-button uk-button-danger"
                   value="Hủy">
            <input type="submit" name="btn_submit" onclick="confirmCreateInvoice();" class="uk-button uk-button-primary"
                   value="Tạo đơn hàng">
        </div>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>